# import requests
# from lxml import etree
# res = requests.get('https://www.gushiwen.cn/user/collect.aspx?type=s&id=6742538&sort=t', headers={
#     # 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36',
#     'cookie': 'ticketStr=201555271%7cgQFG8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAydDhnWFJCbGVkN2kxbzBSNGhGMXgAAgQAKB1pAwQAjScA; ASP.NET_SessionId=f1ffb40r5mirvoep43caatbd; codeyz=e9fa43245b09d452; Hm_lvt_9811648d1409c4608cf19093200cee83=1763456383,1763517875; Hm_lvt_9007fab6814e892d3020a64454da5a55=1762155240,1763456383,1763517875; gsw2017user=6742538%7c30B091B00BDDA892A8390070D387A10F%7c2000%2f1%2f1%7c2000%2f1%2f1; login=flase; wxopenid=defoaltid; gswZhanghao=; gswPhone=15138001200; idsShiwen2017=%2c71477%2c1015027%2c; Hm_lpvt_9811648d1409c4608cf19093200cee83=1763518500; Hm_lpvt_9007fab6814e892d3020a64454da5a55=1763518500'
# })
#
# tree = etree.HTML(res.text)
#
# items = tree.xpath('//div[@class="sons"]/div[@class="cont"]/a')
# for item in items:
#     print(item.xpath('./@href'), item.xpath('./text()'))





# 发起API请求 获取验证码   观察返回值和cookie等信息
import requests
from lxml import etree
from util.time_stamp import get_time_stamp
from ddddocr import DdddOcr

ocr = DdddOcr(show_ad=False)




# 1. 获取隐藏域
res_login_get = requests.get('https://www.gushiwen.cn/user/login.aspx')
tree = etree.HTML(res_login_get.text)


__VIEWSTATE = tree.xpath('//input[@id="__VIEWSTATE"]/@value')[0]
__VIEWSTATEGENERATOR = tree.xpath('//input[@id="__VIEWSTATEGENERATOR"]/@value')[0]




while True:
    # 2. 解析验证码
    res_img = requests.get(f'https://www.gushiwen.cn/RandCode.ashx?t={get_time_stamp()}')

    # print(res.content)
    with open(f'./static/yzm/古诗文.jpg', 'wb') as f:
        f.write(res_img.content)

    r = ocr.classification(res_img.content)
    print(f"识别结果 {r}")
    print(type(res_img.cookies))

    # 3. 发起登录请求
    res_login_post = requests.post('https://www.gushiwen.cn/user/login.aspx', data={
        '__VIEWSTATE': __VIEWSTATE,
        '__VIEWSTATEGENERATOR': __VIEWSTATEGENERATOR,
        'email': '15138001200',
        'pwd': 'Qikuedu123',
        'denglu': '登录',
        'code': f'{r}'
    }, cookies=res_img.cookies)   # 携带cookie 是为了携带验证码 对应的cookie codeyz

    if "您输入的验证码有误" in res_login_post.text:
        print(f"验证码识别失败， 刷新验证码再次识别 ")
    else:
        break


print(res_login_post.cookies)


# 4. 获取收藏信息

res_collect = requests.get('https://www.gushiwen.cn/user/collect.aspx', cookies=res_login_post.cookies)

tree = etree.HTML(res_collect.text)

items = tree.xpath('//div[@class="sons"]/div[@class="cont"]/a')
for item in items:
    print(item.xpath('./@href'), item.xpath('./text()'))

